package com.iweb.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 跨域请求的配置
 */
@Configuration
public class CorsConfig {
    private CorsConfiguration buildConfig() {
        CorsConfiguration conf = new CorsConfiguration();
        conf.addAllowedHeader("*");// 开发任何header
        conf.addAllowedOrigin("*");// 开发任何域名,这里跨域指定IP或者域名
        conf.addAllowedMethod("*");// 开发任何方式GET POST ...
        conf.setAllowCredentials(true);
        return conf;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }
}
